//
//  AlertZKView.m
//  ZhuiKe55like
//
//  Created by junseek on 14-12-1.
//  Copyright (c) 2014年 五五来客 李江. All rights reserved.
//

#import "AlertZKView.h"


#import "RHMethods.h"
#import "NSString+expanded.h"

@interface AlertZKView ()<UITableViewDataSource,UITableViewDelegate>{
    //
    UIView *view_userContact;
    UIView *headViewTitle;
    UITableView *tableViewT;
    UILabel *lblT;
    UIView *viewTableBG;
    UIButton * btnCancel;
}

@property(strong,nonatomic)UITextField *selecttextField;

@end



@implementation AlertZKView


-(instancetype)init{
    self=[super init];
    if (self) {
        [self loadDataView];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self loadDataView];
        
    }
    return self;
}
-(void)loadDataView{
    self.backgroundColor=RGBACOLOR(0, 0, 0, 0.4);
    self.frame=[UIScreen mainScreen].bounds;
    _hiddenAnimate=YES;
    _hiddenFullScreenCliked=YES;
    
    UIControl *closeC=[[UIControl alloc]initWithFrame:self.bounds];
    [closeC addTarget:self action:@selector(closeButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeC];
    
    view_userContact=[[UIView alloc]initWithFrame:CGRectMake(10, -300, kScreenWidth-20, 256)];
    [self addSubview:view_userContact];
    view_userContact.backgroundColor=[UIColor clearColor];//RGBACOLOR(255, 255, 255, 0.9);//
    view_userContact.clipsToBounds=YES;
    
    viewTableBG=[[UIView alloc] initWithFrame:CGRectMake(0, 0, W(view_userContact),H(view_userContact)-54)];
    viewTableBG.backgroundColor=[UIColor whiteColor];
    [view_userContact addSubview:viewTableBG];
    viewTableBG.layer.masksToBounds = YES;
    viewTableBG.layer.cornerRadius =5;
    viewTableBG.layer.borderWidth=1.0;
    viewTableBG.layer.borderColor =[[UIColor clearColor] CGColor];
    
    
    
    headViewTitle=[[UIView alloc]initWithFrame:CGRectMake(0, 0, W(view_userContact), 44)];
    headViewTitle.backgroundColor=rgbGray;//[UIColor redColor];//
    [viewTableBG addSubview:headViewTitle];
    
    lblT=[[UILabel alloc]initWithFrame:CGRectMake(0, Y(headViewTitle)+12, W(headViewTitle), 20)];
    lblT.backgroundColor=[UIColor clearColor];
    lblT.textAlignment=NSTextAlignmentCenter;
    lblT.text=@"选择";
    lblT.textColor=RGBACOLOR(100, 100, 100, 1);//[UIColor whiteColor];
    lblT.font=Font(20);
    [headViewTitle addSubview:lblT];

    
    UIImageView *cellLine=[RHMethods imageviewWithFrame:CGRectMake(0, 43, W(headViewTitle), 1) defaultimage:@"userLine.png"];
    [cellLine setContentMode:UIViewContentModeScaleAspectFill];
    cellLine.clipsToBounds=YES;
    [headViewTitle addSubview:cellLine];
   
    
    //table
    tableViewT=[[UITableView alloc]initWithFrame:CGRectMake(0, 44, W(view_userContact), H(viewTableBG)-44) style:UITableViewStylePlain];
//    [tableViewT setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    tableViewT.delegate=self;
    tableViewT.dataSource = self;
    tableViewT.backgroundColor=rgbGray;//[UIColor clearColor];
    [tableViewT setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [viewTableBG addSubview:tableViewT];
    
   
//    [tableViewT setTableHeaderView:headView];
    
    
    
     btnCancel=[RHMethods buttonWithFrame:CGRectMake(0, YH(tableViewT)+10, W(view_userContact), 44) title:@"取消" image:@"" bgimage:@"btn03TC"];
    btnCancel.titleLabel.font=Font(20);
   // btnCancel.backgroundColor=rgbGray;
    [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [btnCancel addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
//    [btnCancel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
    [view_userContact addSubview:btnCancel];
    
//    btnCancel.layer.masksToBounds = YES;
//    btnCancel.layer.cornerRadius =5;
//    btnCancel.layer.borderWidth=1.0;
//    btnCancel.layer.borderColor =[[UIColor clearColor] CGColor];
//    
    
    
    
    
    
}

#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.userArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier;
    NSDictionary *dic=[self.userArray objectAtIndex:indexPath.row];
    identifier= dic.description.md5;//@"tableMessageCell";
    
    BOOL abool=NO;
    if ( [[self.userArray objectAtIndex:indexPath.row] isKindOfClass:[NSDictionary class]]) {
        abool=YES;
    }
    
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        // [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setBackgroundColor:[UIColor clearColor]];
        UIView *bgView=[[UIView alloc]initWithFrame:cell.bounds];
        bgView.backgroundColor=[UIColor clearColor];//270
        [cell setBackgroundView:bgView];
        
//        UIView *bgViewse=[[UIView alloc]initWithFrame:cell.bounds];
//        bgViewse.backgroundColor=rgbBlue;//RGBACOLOR(46, 95, 253, 0.9);//[UIColor clearColor];
//        [cell setSelectedBackgroundView:bgViewse];
        
        
        UILabel *lblTitle=[RHMethods labelWithFrame:CGRectMake(10, 12, W(view_userContact)-20, 20) font:Font(20) color:rgbpublicColor text:abool?[dic valueForJSONStrKey:@"name"]:[self.userArray objectAtIndex:indexPath.row]];
        [lblTitle setHighlightedTextColor:[UIColor whiteColor]];
        [lblTitle setTextAlignment:NSTextAlignmentCenter];
        [cell addSubview:lblTitle];
        
        if (indexPath.row!=self.userArray.count-1) {
            UIImageView *cellLine=[RHMethods imageviewWithFrame:CGRectMake(0, 43.5, W(tableViewT), 0.5) defaultimage:@"userLine.png"];
            [cellLine setContentMode:UIViewContentModeScaleAspectFill];
            cellLine.clipsToBounds=YES;
            [cell addSubview:cellLine];
        }
    }
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(AlertSelectZKView:clickedButtonAtIndex:)]) {
        [self.delegate AlertSelectZKView:self clickedButtonAtIndex:indexPath.row];
    }
    if (_hiddenAnimate) {
        [self hidden];        
    }else{
        [self hiddenSelect];
    }
}


#pragma mark but
-(void)closeButtonClicked{
    if (_hiddenFullScreenCliked) {
        [self hidden];
    }
}
-(void)cancelButtonClicked{
    //
     [self hidden];
    if ([self.delegate respondsToSelector:@selector(AlertSelectZKViewCancel:)]) {
        [self.delegate AlertSelectZKViewCancel:self];
    }
}
- (void)show
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidden) name:@"push_showLogin" object:nil];
    
    lblT.text=self.strTitle;
    
    float tempF=(self.userArray.count) * 44.0;
    self.strIphoneIP=[[Utility Share] getIPAddress];
    DLog(@"ip:%@",_strIphoneIP)
    
    float tempY=tempF>(kScreenHeight-100-98)?(kScreenHeight-100-98):tempF;
    view_userContact.frame=CGRectMake(X(view_userContact),kScreenHeight, W(view_userContact),tempY+ 98);
    tableViewT.frame=CGRectMake(0, 44, W(view_userContact), tempY);
    viewTableBG.frameHeight=tempY+44;
    btnCancel.frameY=YH(viewTableBG)+10;
    DLog(@"____________%f",tempY);
    [tableViewT reloadData];
   
    // [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
    self.hidden = NO;
    self.alpha = 0.0f;
   
    [UIView animateWithDuration:0.3
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.alpha = 1.0f;
                         [view_userContact setFrame:CGRectMake(X(view_userContact), (kScreenHeight-H(view_userContact))-15, W(view_userContact), H(view_userContact))];
                         
                     }
                     completion:^(BOOL finished) {
                         
                     }];
    
}

- (void)hidden
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.alpha = 0.0;
                             view_userContact.frame=CGRectMake(X(view_userContact),kScreenHeight , W(view_userContact),H(view_userContact));
                             
                         }
                         completion:^(BOOL finished) {
                             self.hidden=YES;
                         }];
   
    
   

//    [UIView animateWithDuration:0.4 animations:^{
//        self.alpha = 0.0;
//        view_userContact.frame=CGRectMake(X(view_userContact),kScreenHeight , W(view_userContact),H(view_userContact));
//    }];
    
}

- (void)hiddenSelect
{
    
    self.hidden=YES;
    
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
